package com.dhsc.service.impl.xtqxgl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhsc.domain.ywzx.xtqxgl.BqcsglVo;
import com.dhsc.domain.ywzx.xtqxgl.RysxVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.domain.yypt.sys.TXtdmVo;
import com.dhsc.domain.yypt.yhzx.TYhxxVo;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.xtqxgl.IBqcsglDao;
import com.dhsc.local.dao.yypt.ITxtdmDao;
import com.dhsc.local.dao.yypt.IYhzxDao;
import com.dhsc.service.xtqxgl.IBqcsglService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: BqcsglServiceImpl
 * @Description: 标签参数管理
 * @date: 2023/11/8 9:32
 * @author: gx
 */
@Service
@Slf4j
public class BqcsglServiceImpl implements IBqcsglService {

    @Autowired
    private IBqcsglDao bqcsglDao;

    @Autowired
    private IYhzxDao yhzxDao;

    @Autowired
    private ITxtdmDao txtdmDao;

    @Override
    public LayuiTableVo queryBqcsgl(BqcsglVo bqcsglVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(bqcsglVo.getPage(), bqcsglVo.getLimit());
            List<BqcsglVo> list = bqcsglDao.queryBqcsgl(bqcsglVo);

            TXtdmVo txtdmVo = new TXtdmVo();
            txtdmVo.setFldmstr("TBQ_BQLB,TBQ_JGDM");
            List<TXtdmVo> txtdmVos = txtdmDao.queryTXtdmByFldm(txtdmVo);
            List<TYhxxVo> tYhxxVos = yhzxDao.queryTYhxx(new TYhxxVo());
            if(CommonUtil.isNotEmptyList(list))
            {
            	for (int i = 0; i < list.size(); i++) {
                    BqcsglVo bqcsgl = list.get(i);
                    //遍历字典，进行翻译
                    for (int j = 0; j < txtdmVos.size(); j++) {
                        TXtdmVo txtdm = txtdmVos.get(j);
                        if (txtdm.getFldm().equals("TBQ_BQLB") && txtdm.getBm().equals(String.valueOf(bqcsgl.getBqlb()))){
                        	bqcsgl.setBqlbmc(txtdm.getBmsm());
                        }
                        if (txtdm.getFldm().equals("TBQ_JGDM") && txtdm.getBm().equals(bqcsgl.getJgdm())){
                        	bqcsgl.setJgdmmc(txtdm.getBmsm());
                        }
                    }
                    //遍历用户信息，进行翻译
                    for (int k = 0; k < tYhxxVos.size(); k++) {
                        TYhxxVo tYhxxVo = tYhxxVos.get(k);
                        if (tYhxxVo.getTruename().equals(bqcsgl.getCjr())){
                            bqcsgl.setTruename(tYhxxVo.getTruename());
                        }
                        if (tYhxxVo.getTruename().equals(bqcsgl.getBdr())){
                            bqcsgl.setBdrtruename(tYhxxVo.getTruename());
                        }
                    }
                }
            }
            PageInfo<BqcsglVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询标签参数管理信息出错",e);
            throw new MyException("查询标签参数管理信息出错",e);
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject saveBqcsgl(BqcsglVo bqcsglVo) {
        ResponseObject obj = new ResponseObject();
        try {
            if (!CommonUtil.isNotEmptyObj(bqcsglVo.getBh())){
                int bh = bqcsglDao.queryBqlxSequences();
                bqcsglVo.setBh(bh);
                bqcsglDao.insertBqlx(bqcsglVo);
                if (CommonUtil.isNotEmptyObj(bqcsglVo.getJgdm())){
                    //如果已分发部门不为空，则继续插入xy.tbq_bq_jgdm表数据
                    bqcsglDao.insertJgdm(bqcsglVo);
                }
            }else{
                bqcsglDao.updateBqlx(bqcsglVo);
                //先删除已分发部门
                bqcsglDao.deleteJgdm(bqcsglVo);
                if (CommonUtil.isNotEmptyObj(bqcsglVo.getJgdm())){
                    //如果已分发部门不为空，则继续插入xy.tbq_bq_jgdm表数据
                    bqcsglDao.insertJgdm(bqcsglVo);
                }
            }
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("保存标签参数管理信息出错",e);
            throw new MyException("保存标签参数管理信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryRysx(RysxVo rysxVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(rysxVo.getPage(), rysxVo.getLimit());
            List<RysxVo> list = bqcsglDao.queryRysx(rysxVo);
            PageInfo<RysxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询人员属性信息出错",e);
            throw new MyException("查询人员属性信息出错",e);
        }
        return obj;
    }
}
